package com.loan.mappers;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.loan.entity.Application;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

/**
 * 申请表mapper
 */
public interface ApplicationMapper extends BaseMapper<Application> {
    /**
     * 条件查询申请表;带出所有信息
     * @return
     */
    @Results(value = {
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "customerId",column = "customer_id"),
            @Result(property = "customer",
                    column = "customer_id",
                    one = @One(select = "com.loan.mappers.CustomerMapper.findAllInfoById")),
            @Result(property = "proId",column = "pro_id"),
            @Result(property = "product",
                    column = "pro_id",
                    one = @One(select = "com.loan.mappers.ProductMapper.selectById")),
            @Result(property = "money",column = "money"),
            @Result(property = "applyTime",column = "apply_time"),
            @Result(property = "status",column = "status"),
            @Result(property = "acceptAccount",column = "accept_account"),
            @Result(property = "mortgageList",
                    column = "id",
                    many = @Many(select = "com.loan.mappers.MortgageMapper.findByAppId")),
            @Result(property = "processList",
                    column = "id",
                    many = @Many(select = "com.loan.mappers.ProcessMapper.findByAppid")),
    })
    @SelectProvider(type = SqlProvider.class,method = "selectByCondition")
    List<Application> findByCondition(Application application);

    public static class SqlProvider {
        public static String selectByCondition(Application application) {
            SQL sql = new SQL();
            sql.SELECT("*").FROM("application");
            if (application==null){
                return sql.toString();
            }
            if (application.getId() != null){
                sql.WHERE("id=#{id}");
            }
            if (application.getCustomerId() != null){
                sql.WHERE("cus_id=#{cusId}");
            }
            if (application.getProId() != null){
                sql.WHERE("pro_id=#{proId}");
            }
            if (application.getStatus() != null){
                sql.WHERE("status=#{status}");
            }
            return sql.toString();
        }
    }
}
